#include "WindowManager.h"
#include "TalkWindow.h"
#include <QSqlQueryModel>

WindowManager* WindowManager::manage = nullptr;

WindowManager::WindowManager()
	:QObject(nullptr),
	m_talkwindowshell(nullptr)
{
}

WindowManager::~WindowManager()
{
}

void WindowManager::addNewTalkWindow(const QString & qWindowId)
{
	if (m_talkwindowshell == nullptr)
	{
		m_talkwindowshell = new TalkWindowShell;

		connect(m_talkwindowshell, &TalkWindowShell::destroyed, [this](QObject*)
		{
			m_talkwindowshell = nullptr;
		});
	}

	QWidget *w = findWindowId(qWindowId);
	if (!w)
	{	
		WindowId = qWindowId;
		TalkWindow *talkWindow = new TalkWindow(m_talkwindowshell, qWindowId);
		talkWindow->setID(qWindowId);
		WindowId = "";

		TalkWindowItem *talkWindowItem = new TalkWindowItem(talkWindow);

		QSqlQueryModel qModel;
		const QString &sql = QString(
			"SELECT department_name, sign , picture FROM tab_department WHERE departmentID=%1;"
		).arg(qWindowId);

		QString name, sign, path;
		qModel.setQuery(sql);
		if (qModel.rowCount() == 0)
		{
			const QString &sql1 = QString(
				"SELECT employee_name, employee_sign , picture FROM tab_employees WHERE employeeID=%1;"
			).arg(qWindowId);

			qModel.setQuery(sql1);

			//talkWindow->setGroupTalk(false);
		}
		name = qModel.data(qModel.index(0, 0)).toString();
		sign = qModel.data(qModel.index(0, 1)).toString();
		path = qModel.data(qModel.index(0, 2)).toString();

		talkWindowItem->setMsgLabelContent(name);
		talkWindowItem->setHeadPixmap(path);

		talkWindow->setWindowName(name);
		talkWindow->setSignName(sign);
		

		/*switch (type)
		{
		case COMPANY:
			talkWindow->setWindowName("123456");
			talkWindowItem->setMsgLabelContent("一");
			break;
		case PERSONELGROUP:
			talkWindow->setWindowName("23456");
			talkWindowItem->setMsgLabelContent("二");
			break;
		case DEVELOPMENTGROUP:
			talkWindow->setWindowName("3456");
			talkWindowItem->setMsgLabelContent("三");
			break;
		case MARKETGROUP:
			talkWindow->setWindowName("456");
			talkWindowItem->setMsgLabelContent("四");
			break;
		case PTOP:
			talkWindow->setWindowName("56");
			talkWindowItem->setMsgLabelContent("五");
			break;
		case USER:
			talkWindow->setWindowName("6");
			talkWindowItem->setMsgLabelContent("六");
			break;
		default:
			break;
		}*/

		m_talkwindowshell->addTalkWindow(talkWindow, talkWindowItem, qWindowId);
		//m_windowMap.insert(qWindowId, talkWindow);
		//addWindowId(qWindowId, talkWindow);
	}
	else
	{
		//m_talkwindowshell->setCurrendWidget(w);

		m_talkwindowshell->onTalkWindowItemChicked(
			m_talkwindowshell->getTalkWindowItemMap().key(w)
		);
	}

	m_talkwindowshell->show();
	m_talkwindowshell->activateWindow();

	//m_talkwindowshell->setCurrendWidget(w);
}

WindowManager * WindowManager::getInstance()
{
	if (manage == nullptr) {
		manage = new WindowManager;
	}

	return manage;
}

QWidget * WindowManager::findWindowId(const QString & qWindowId)
{
	if (m_windowMap.contains(qWindowId))
		return m_windowMap.value(qWindowId);
	else
		return nullptr;
}

void WindowManager::deleteWindowId(const QString & qWindowId)
{
	m_windowMap.remove(qWindowId);
}

void WindowManager::addWindowId(const QString & qWindowId, QWidget * qWidget)
{
	if (!m_windowMap.contains(qWindowId))
	{
		m_windowMap.insert(qWindowId, qWidget);
	}
}

QString WindowManager::getWindowId()
{
	return WindowId;
}
